﻿
var events = WinJS.Namespace.define("Events", {
    attachEvents: function () {
        var showRegister = document.getElementById("showRegister");
        var showLogin = document.getElementById("showLogin");
        var errors = document.getElementById("errorMessage");
        
        showRegister.addEventListener("click", function () {
            showLogin.className = "";
            document.getElementById("loginForm").style.display = "none";
            showRegister.className = "onFocus";
            document.getElementById("registerForm").style.display = "block";
        });

        showLogin.addEventListener("click", function () {
            showRegister.className = "";
            document.getElementById("registerForm").style.display = "none";
            showLogin.className = "onFocus";
            document.getElementById("loginForm").style.display = "block";
        });

        var loginButton = document.getElementById("btnLogin");
        loginButton.addEventListener("click", function () {
            var username = document.getElementById("username").value;
            var password = document.getElementById("password").value;

            var escapedUsername = escapeSting(username);
            var authCode = CryptoJS.SHA1(password).toString();

            WinJS.xhr({
                type: "POST",
                url: "http://weatherapp.apphb.com/api/users/login/",
                headers: { "Content-type": "application/json" },
                data: JSON.stringify({ Username: escapedUsername, AuthCode: authCode })
            }).then(
                function (success) {
                    var applicationData = Windows.Storage.ApplicationData.current;
                    var localSettings = applicationData.localSettings;
                    var sessionKey = localSettings.values["sessionKey"] =
                        JSON.parse(success.responseText).SessionKey;
                    ui.generateUI();
                },
                function (error) {
                    errors.innerHTML=error.responseText;
                });

        });

        var registerButton = document.getElementById("btnRegister");
        registerButton.addEventListener("click", function () {
            var username = document.getElementById("regUsername").value;
            var name = document.getElementById("fullName").value;
            var password = document.getElementById("regPassword").value;

            var escapedUsername = escapeSting(username);
            var escapedName = escapeSting(name);
            var authCode = CryptoJS.SHA1(password).toString();
            WinJS.xhr({
                type: "POST",
                url: "http://weatherapp.apphb.com/api/users/register/",
                headers: { "Content-type": "application/json" },
                data: JSON.stringify({ Username: escapedUsername, AuthCode: authCode, Name: escapedName })
            }).then(
                function (success) {
                    var applicationData = Windows.Storage.ApplicationData.current;
                    var localSettings = applicationData.localSettings;
                    var sessionKey = localSettings.values["sessionKey"] =
                        JSON.parse(success.responseText).SessionKey;
                    ui.generateUI();
                },
                function (error) {
                    errors.innerHTML = error.responseText;
                });
        });

        var logoutButton = document.getElementById("btnLogout");
        logoutButton.addEventListener("click", function () {

            var applicationData = Windows.Storage.ApplicationData.current;
            var localSettings = applicationData.localSettings;
            var sessionKey = localSettings.values["sessionKey"];
            WinJS.xhr({
                type: "PUT",
                url: "http://weatherapp.apphb.com/api/users/logout/" + sessionKey,
                headers: { "Content-type": "application/json" },
            }).then(
               function (success) {
                   var sessionKey = localSettings.values["sessionKey"] = "";
                   ui.generateUI();
               },
               function (error) {
                   errors.innerHTML = error.responseText;
               });
        });

    }
});

var escapeSting = (function () {
    var chr = {
        '"': '&quot;', '&': '&amp;', "'": '&#39;',
        '/': '&#47;', '<': '&lt;', '>': '&gt;'
    };
    return function (text) {
        return text.replace(/[\"&'\/<>]/g, function (a) { return chr[a]; });
    };
}());

